package server

import (
	"zk2/common/models"
	"zk2/common/pkg"
	"zk2/common/proto/user"
)

func Reg(in *user.RegRequest) (*user.RegResponse, error) {
	u := models.User{
		Username: in.Username,
		Password: in.Password,
		Mobile:   in.Mobile,
	}
	code := "1234"
	pkg.SendSms(in.Mobile, code)
	err := u.Reg()
	if err != nil {
		return nil, err
	}
	return &user.RegResponse{
		UserId: int64(u.ID),
	}, nil
}

func Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	login, err := models.Login(in.Mobile)
	code := "1234"
	pkg.SendSms(in.Mobile, code)
	if err != nil {
		return nil, err
	}
	return &user.LoginResponse{
		UserId:   int64(login.ID),
		Username: login.Username,
		Password: login.Password,
		Mobile:   login.Mobile,
	}, nil
}
